%main = getBranches(main)
%   This function gets the positions the seperate branches are connected to
%   and stores them into the field 'branchorigin' in 'main' before adding
%   the numeric position of the first element of the branch to the field
%   'hasBranch' of the origin of that branch. It also puts the correct info
%   in the field 'bonds' regarding the bonds in between branchelements.
%   After adding these fields, it returns the edited struct 'main'.

%%

function main = getBranches(main)

%% Initialising
%Get all the branches and store them in the struct 'branch'. A position
%j in this struct corrosponds with a branch j-1 in which branch 0 is the
%main branch.
for j = 1:max([main.branch])+1
    branch(j).elements = find([main.branch] == j-1);
end

%% Main

%% Finding the origin
%Set the origin of the main branch to be 1 and therefore create the field
%'origin' within the struct 'branch'
branch(1).origin = 1;
%For all branches but the main branch
for j = 2:length(branch)
    %Create an empty 'badx'
    badx = [];
    %For all elements from the end of the current branch to the first
    %element in 'main':
    for k = branch(j).elements(1):-1:1
        %For all branches
        for l = 1:length(branch)
            %If the current branch 'l' contains the current position 'k',
            %store 'l' in 'x' and break
            if ~isempty(find([branch(l).elements] == k,1))
                x = l;
                break
            end
        end
        %If the branch of the current position 'k' is lower than the branch
        %of the first member of branch 'j' AND the current branch has not
        %been added to the list 'badx' AND ('k' is not the last of branch
        %'x' or the current branch 'x' IS the first): break: The origin has
        %been found!
        if main(k).branch < main(branch(j).elements(1)).branch && ...
                isempty(find(badx == x,1)) && ...
                (k ~= branch(x).elements(length(branch(x).elements)) ...
                || x == 1)
            break
        else
            %If all that is not true, branch 'j' is not attached to 'k' and
            %none of it's members will be so we store this branch in
            %'badx'.
            badx = [badx x];
        end
    end
    %After finding the origin, set the field 'origin' for this branch to
    %'k'.
    branch(j).origin = k;
end

%% Store branchorigins into 'main'
%For all elements in 'main'
for j = 1:length(main)
    %If the element is in a branch other than the main branch
    if main(j).branch
        %Store the found origin into the field 'branchorigin' of the struct
        %'main'.
        main(j).branchorigin = branch(main(j).branch+1).origin;
        %If 'j' is the first of it's branch
        if branch(main(j).branch+1).elements(1) == j
            %If the field 'hasBranch' does not yet exist store 'j' in it
            %for the branchorigin of this branch
            if ~myIsField(main,'hasBranch')
                main(main(j).branchorigin).hasBranch = j;
            else
                %Else add 'j' after the existing field for the origin of
                %it's branch.
                main(main(j).branchorigin).hasBranch = ...
                    [main(main(j).branchorigin).hasBranch j];
            end
        end
    else
        %If the element does not have a branch, set it's branchorigin to 0.
        main(j).branchorigin = 0;
    end
end

%% Bonding branchelements
%For all the branches
for j = 1:length(branch)
    %For all a branches members but the last
    for k = 1:length(branch(j).elements)-1
        %Add the next element in the branch to the field 'bonds' of the
        %current element 'k'
        main(branch(j).elements(k)).bonds = ...
            [main(branch(j).elements(k)).bonds branch(j).elements(k+1)];
    end
end

%% End
end